package com.learn.api.gateway.web;

import com.learn.api.gateway.service.GatewayDynamicRouteService;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author lwt<br>
 * @description <br>
 * @date 2020/8/17
 */
@RestController
@RequestMapping("/gateway")
public class GatewayDynamicRouteController {

    @Resource
    private GatewayDynamicRouteService gatewayDynamicRouteService;

    @GetMapping("/test")
    public String test() {
        return "helloWord";
    }

    /**
     * {
     *    "id":"learn-service-first-route",
     *    "predicates":[
     *       {
     *          "name":"Path",
     *          "args":{
     *             "_genkey_0":"/learn-service-first/**"
     *          }
     *       }
     *    ],
     *    "filters":[
     *       {
     *          "name":"StripPrefix",
     *          "args":{
     *             "_genkey_0":"1"
     *          }
     *       },
     *            {
     *          "name":"Config",
     *          "args":{
     *             "printTestInfo":"false"
     *          }
     *       }
     *    ],
     *    "uri":"lb://learn-service-first-service"
     * }
     * @param entity
     * @return
     */
    @PostMapping("/add")
    public String create(@RequestBody RouteDefinition entity) {
        int result = gatewayDynamicRouteService.add(entity);
        return String.valueOf(result);
    }

    @PostMapping("/update")
    public String update(@RequestBody RouteDefinition entity) {
        int result = gatewayDynamicRouteService.update(entity);
        return String.valueOf(result);
    }

    @DeleteMapping("/delete/{id}")
    public Mono<ResponseEntity<Object>> delete(@PathVariable String id) {
        return gatewayDynamicRouteService.delete(id);
    }

}
